# -*- mode: python; python-indent-offset: 4; indent-tabs-mode: nil -*-
##
# script to check if Google Unit Test library is installed.
# If not, get it and install it
#
##

import os
Import('env')
target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')
build_dir = env.get('BUILD_DIR')

targets_need_gtest = ['darwin', 'linux', 'msys_nt', 'windows']

GTEST_VERSION = '1.7.0'
gtest_dir = os.path.join(src_dir, 'extlibs', 'gtest',
                         'googletest-release-' + GTEST_VERSION)
gtest_lib_dir = os.path.join(gtest_dir, 'lib')
gtest_configured_sentinel = os.path.join(gtest_dir, 'Makefile')
gtest_unpacked_sentinel = os.path.join(gtest_dir, 'CMakeLists.txt')
gtest_dotlib_dir = os.path.join(gtest_lib_dir, '.libs')
gtest_zip_file = 'release-' + GTEST_VERSION + '.zip'
gtest_url = 'https://github.com/google/googletest/archive/' + gtest_zip_file
gtest_zip_path = os.path.join(src_dir, 'extlibs', 'gtest', gtest_zip_file)

gtest_env = env.Clone()
# nothing to do if this target doesn't use gtest
if target_os not in targets_need_gtest:
    Return("gtest_env")

# nothing to do if asked for help
if gtest_env.GetOption('help'):
    Return("gtest_env")

# Clean up gtest if 'clean' is specified, and it looks like has been built.
# Implicity skips the targets which don't "configure" (e.g. windows)
if gtest_env.GetOption('clean'):
    print('Cleaning google unit test')
    if os.path.exists(gtest_configured_sentinel):
        clean = "cd %s && make clean" % gtest_dir
        Execute(clean)
    Return("gtest_env")

print('*** Checking for installation of google unit test %s ***' % GTEST_VERSION)
if not os.path.exists(gtest_unpacked_sentinel):
    # If the gtest zip file is not already present, download it
    if not os.path.exists(gtest_zip_path):
        gtest_zip = gtest_env.Download(gtest_zip_path, gtest_url)
    else:
        gtest_zip = gtest_zip_path
    print('Unzipping to : ' + gtest_dir)
    gtest_env.UnpackAll(gtest_dir, gtest_zip)

if target_os in ['linux']:
    if not os.path.exists(gtest_configured_sentinel):
        # Run configure on gtest
        print('Configuring google unit test')
        reconfigure_ran = gtest_env.Configure(gtest_dir, 'autoreconf -i')
        if env.get('CROSS_COMPILE'):
            configure_ran = env.Configure(
                gtest_dir,
                './configure --disable-shared --host=' + env['CROSS_COMPILE'])
        else:
            configure_ran = env.Configure(gtest_dir,
                                          './configure --disable-shared')
        gtest_env.Depends(configure_ran, reconfigure_ran)

    # Run make on gtest
    print('Making google unit test')
    make_ran = gtest_env.Configure(gtest_dir, 'make')

elif target_os in ['darwin', 'msys_nt'] and os.path.exists(gtest_dir):
    if not os.path.exists(gtest_configured_sentinel):
        # Create lib dir
        os.mkdir(gtest_lib_dir)
        os.mkdir(gtest_dotlib_dir)

        # Run configure on gtest
        print('Configuring google unit test for compilation')
        gtest_env.Configure(gtest_dir, 'cmake -G "Unix Makefiles" .')

    # Run make on gtest
    print('Making google unit test')
    gtest_env.Configure(gtest_dir, 'make')

    print('Moving libraries to lib folder')
    gtest_env.Configure(gtest_dir, 'cp libgtest.a lib')
    gtest_env.Configure(gtest_dir, 'mv libgtest.a lib/.libs')
    gtest_env.Configure(gtest_dir, 'cp libgtest_main.a lib')
    gtest_env.Configure(gtest_dir, 'mv libgtest_main.a lib/.libs')

elif target_os in ['windows'] and 'IOTIVITY_GTEST_HAS_BEEN_BUILT' not in env:
    # Avoid building the same StaticLibrary in more than one environment,
    # by using the IOTIVITY_GTEST_HAS_BEEN_BUILT environment variable
    gtest_env.AppendUnique(CPPPATH=[gtest_dir])
    gtest_build_env = gtest_env.Clone()
    gtest_build_env.AppendUnique(CPPPATH=[os.path.join(gtest_dir, 'include')])
    gtest_build_env.AppendUnique(CXXFLAGS=['/EHsc'])
    gtest_build_env.AppendUnique(CCFLAGS=['/W4', '/WX'])
    gtest = gtest_build_env.StaticLibrary(target=build_dir + 'gtest',
                           source=gtest_dir + '/src/gtest-all.cc')
    gtest_main = gtest_build_env.StaticLibrary(target=build_dir + 'gtest_main',
                           source=gtest_dir + '/src/gtest_main.cc')

    gtest_vars = Variables()
    gtest_vars.AddVariables(('IOTIVITY_GTEST_HAS_BEEN_BUILT', '', '1'))
    gtest_vars.Update(env)

# Export flags once for all
gtest_env.AppendUnique(LIBPATH=[gtest_dotlib_dir])
gtest_env.PrependUnique(CPPPATH=[os.path.join(gtest_dir, 'include')])
gtest_env.AppendENVPath('LD_LIBRARY_PATH', gtest_dotlib_dir)
if 'g++' in gtest_env.get('CXX'):
    gtest_env.AppendUnique(CXXFLAGS=['-std=c++0x'])
    gtest_env.AppendUnique(CXXFLAGS=['-Wall'])
    # Note: 'pthread' for android is in bionic
    # On other platforms, if use new gcc(>4.9?) it isn't required,
    # otherwise, it's required
    if target_os not in ['android']:
        gtest_env.AppendUnique(CXXFLAGS=['-pthread'])
        gtest_env.PrependUnique(LIBS=['pthread'])
gtest_env.PrependUnique(LIBS=['gtest', 'gtest_main'])
if target_os in ['windows']:
    gtest_env.AppendUnique(LINKFLAGS=['/subsystem:CONSOLE'])

Return('gtest_env')
